package com.jdz.gxq.info.admin.training.service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import jp.terasoluna.fw.dao.QueryDAO;
import jp.terasoluna.fw.dao.UpdateDAO;

import com.jdz.gxq.info.admin.training.bo.Training;

public class TrainingServiceImpl implements TrainingService {

	private QueryDAO queryDAO = null;

	private UpdateDAO updateDAO = null;

	/**
	 * Training检索分页用，查询记录条数
	 * 
	 * @return
	 */
	public int getTrainingsCount(String enterpriseName) {

		int totalCount = queryDAO.executeForObject(
				"training.getTrainingsCount", enterpriseName, Integer.class);

		return totalCount;
	}

	/**
	 * Training检索
	 * 
	 * @return
	 */
	public List<Training> getTrainings(Map<String, Object> params) {

		return queryDAO.executeForObjectList("training.getTrainings", params);
	}

	/**
	 * 追加Training
	 * 
	 * @param training
	 */
	public void insertTraining(Training training) {

		updateDAO.execute("training.insertTraining", training);
	}

	/**
	 * 更新Training
	 * 
	 * @param training
	 */
	public void updateTraining(Training training) {

		updateDAO.execute("training.updateTraining", training);
	}

	/**
	 * 通过id得到training
	 * 
	 * @param id
	 * @return
	 */
	public Training getTrainingById(int id) {

		Training training = queryDAO.executeForObject(
				"training.getTrainingById", id, Training.class);

		return training;
	}

	/**
	 * 删除Training
	 * 
	 * @param training
	 */
	public void deleteTraining(int id) {

		updateDAO.execute("training.deleteTrainingById", id);
	}

	/**
	 * 验证taxName是否重复 flag: 1 追加时验证 flag: 2 更新时验证
	 */
	public boolean checkTrainingName(Training paramTraining, int flag) {

		Map<String, Object> params = new HashMap<String, Object>();
		// 年度
		params.put("trainingTime", paramTraining.getTrainingTime());

		// 企业名称
		params.put("enterpriseName", paramTraining.getEnterpriseName());

		Training training = queryDAO.executeForObject(
				"training.getTrainingByNameAndTime", params, Training.class);

		if (training == null) {
			return true;
		}

		// 更新验证，查出来的数据是不是本身
		if (flag == 2) {

			if (training.getId() == paramTraining.getId()) {
				return true;
			}
		}

		return false;
	}

	public void setQueryDAO(QueryDAO queryDAO) {
		this.queryDAO = queryDAO;
	}

	public void setUpdateDAO(UpdateDAO updateDAO) {
		this.updateDAO = updateDAO;
	}

}
